<?php

if(!defined('IN_SYS')) exit('Access Denied');

class spider_controller extends admincp{

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$yuming_id = $this->currentYumingId;

		if (empty($yuming_id)) {
			self::redirect("?m=yuming&a=select");
		}

		$add_time = time()-3600*24*31;
		$spider_types = spider::spider_types();

		$site_spider =  DATA.DS.'spider'.DS.$yuming_id.DS.'spider.php';
		if(!empty($yuming_id) && is_file($site_spider)) {
			$spider = require $site_spider;
		} else {
			$spider = array();
		}

		$list_count = array();
		foreach ($spider as $d => $v) {
			$v['adddate'] = $d;
			foreach ($spider_types as $t => $c) {
				if (empty($v[$t])) {
					$v[$t] = 0;
				}
			}
			$list_count[] = $v;
		}

		require self::tpl('spider');
	}

	public  function spider_list(){
		$yuming_id = gp("yuming_id");
		
		$hour = gp("hour");
		$_p = gp("_p");
		
		$spider_types = spider::spider_types();
		
		$hour = empty($hour) ? date("YmdH") : $hour;
		$_p = empty($_p) ? 1 : $_p;
		
		$pagesize = 15;
		$start = ($_p-1)*$pagesize;
		
		$init_time = time();
		$hourlist = array();

		$site_dir = DATA.DS.'spider'.DS.$yuming_id;
		
		while ($init_time>=strtotime("-1 days")){
			$h = date("YmdH",$init_time);
			$hourfile = $site_dir.DS.$h.".log";
			if(is_file($hourfile)) {
				$hourlist[$h] = date("Y-m-d H",$init_time);
			}
			$init_time -= 3600;
		}
		
		$page = '';
		$list = array();
		$log_file = $site_dir.DS.$hour.".log";

		if(is_file($log_file)) {
			$fp = new SplFileObject($log_file, 'r');
			$fp->seek($start);// 转到第N行, seek方法参数从0开始计数
			
			for($i = 0; $i < $pagesize; ++$i) {
				//$list[] = trim($fp->current());
				$curline = trim($fp->current());	//current()获取当前行内容
				$arr = explode(',',$curline,3);
				if (count($arr)==3) {
					$list[] = array(
						'spider' => $arr[0],
						'addtime' => $arr[1],
						'url' => $arr[2]
					);
				}
				$fp->next();// 下一行
			}
			$fp->seek(filesize($log_file));
			$total = $fp->key();
			$page = getpage($total,$pagesize);
		}

		require self::tpl('spider_list');
	}

	public  function clean(){
		$yuming_id = $this->currentYumingId;

		$site_dir = DATA.DS.'spider'.DS.$yuming_id;

		$dir = @scandir($site_dir);
		foreach ($dir as $v) {
			echo $site_dir.DS.$v.'<br>';
			if(is_file($site_dir.DS.$v)){
				unlink($site_dir.DS.$v);
			}
		}
		self::redirect("?m=spider", "清除成功");
	}
}


function getpage($total, $pagesize){
	$nowpage = gp('_p');
	$totalpage = ceil($total/$pagesize);
	$startpage = $nowpage<=3?1:$nowpage-3;
	$endpage = $nowpage+3>$totalpage?$totalpage:$nowpage+3;
	
	$page = '';
	if($totalpage>1){
		$param = '';
		$_gp = array_merge($_GET, $_POST);
		foreach($_gp as $_k => $_v){
			if($_k!='_p' && is_string($_v)){
				$param .= $_k.'='.stripslashes($_v).'&';
			}
		}
		$nowurl = '';
		$page = '';
		if($nowpage>1) $page .= '<a href="'.$nowurl.'?'.trim($param,'&').'">第一页</a>';
		for($i=$startpage;$i<=$endpage;$i++){
			if($i==$nowpage){
				$page .= '<b>'.($i).'</b>';
			}else if($i==0){
				$page .= '<a href="'.$nowurl.'?'.trim($param,'&').'">'.($i).'</a>';
			}else{
				$page .= '<a href="'.$nowurl.'?'.$param.'_p='.$i.'">'.($i).'</a>';
			}
		}
		if($totalpage>1 && ($nowpage)!=$totalpage) $page .= '<a href="'.$nowurl.'?'.$param.'_p='.($totalpage).'">最后一页</a>';
	}
	$page .= "<span>共{$totalpage}页{$total}条</span>";
	return $page;
}

function readBigFile($filename, $start = 0,  $count = 20, $tag = "\r\n") { 
	$content = "";//最终内容 
	$current = "";//当前读取内容寄存 
	$step= 1;//每次走多少字符 
	$tagLen = strlen($tag); 

	$i = 0;//计数器 
	$handle = fopen($filename,'r+');//读写模式打开文件，指针指向文件起始位置 
	while($i < $count && !feof($handle)) { 
		fseek($handle, $start, SEEK_SET);//指针设置在文件开头 
		$current = fread($handle,$step);//读取文件 
		$content .= $current;//组合字符串 
		$start += $step;//依据步长向前移动 
		//依据分隔符的长度截取字符串最后免得几个字符 
		$substrTag = substr($content, -$tagLen); 
		if ($substrTag == $tag) { //判断是否为判断是否是换行或其他分隔符 
			$i++;
		} 
	} 
	//关闭文件 
	fclose($handle); 
	//返回结果 
	return $content; 
} 

